﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：徐彦龙 
    //
    // 文件名：RoleList.cs
    // 文件功能描述：角色管理，修改角色信息
    // 创建标识：徐彦龙2009-03-09
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using System.Collections.Generic;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin
{
    public partial class ModRole : BasePage
    {
        IBLL_Role ir = Factory.BLL_Role();
        IBLL_OperatingRule ior = Factory.BLL_OperatingRule();

        DataTable allOperateTable;

        //合并单元格时使用
        protected string lastModule = "";

        public ModRole()
        {
            this.ModuleNo = Module.系统管理.ToString();
            this.PageOperateType = OperateType.主用户管理.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //非超级管理员不允许操作此页面
            if (!CurrentAccount.IsSuperAdmin())
            {
                PageError("非超级管理员不允许操作此功能", "");
            }

            string roleId = Request.QueryString["roleId"].ToString();
            if (!this.IsPostBack)
            {
                //获取角色基本信息
                Role role = ir.GetRole_OperatingRule_ByRoleID(roleId);
                if (role != null)
                {
                    lblRoleName.Text = role.RoleName;

                    //获取所有操作权限
                    IList<OperatingRule> allOperatingRuleList = ior.GetAllOperatingRule();
                    //获取角色已有的操作权限
                    IList<OperatingRule> roleOperatingRuleList = role.OperatingRuleList;

                    //转换成Datatable
                    allOperateTable = HZ.Data.ModelConvert.GetDataTableFromModel(allOperatingRuleList);
                    DataTable roleOperateTable = HZ.Data.ModelConvert.GetDataTableFromModel(roleOperatingRuleList);
                    roleOperateTable.PrimaryKey = new DataColumn[] { roleOperateTable.Columns["OperatingRuleID"] };

                    //添加自定义列 合并二张table
                    allOperateTable.Columns.Add(new DataColumn("IsRoles", typeof(System.Boolean)));
                    foreach (DataRow row in allOperateTable.Rows)
                    {
                        row["IsRoles"] = roleOperateTable.Rows.Contains(row["OperatingRuleID"]);
                    }

                    DataTable tableModule = allOperateTable.DefaultView.ToTable(true, "Module", "ComFlag", "CenFlag", "HosFlag");

                    //增加一列统计重复Module行的列
                    tableModule.Columns.Add(new DataColumn("RepeatNum", typeof(System.Int32)));
                    foreach (DataRow row in tableModule.Rows)
                    {
                        row["RepeatNum"] = tableModule.Compute("Count(Module)", "Module='" + row["Module"] + "'");
                    }

                    //添加模块枚举值列
                    tableModule.Columns.Add(new DataColumn("ModuleValue", typeof(System.Int32)));
                    foreach (DataRow row in tableModule.Rows)
                    {
                        row["ModuleValue"] = EnumParse.GetModule((int)row["Module"]);
                    }

                    //按模块枚举值排序
                    DataView dv = new DataView(tableModule);
                    dv.Sort = "ModuleValue asc";

                    //绑定到目标
                    this.rptModule.DataSource = dv;
                    this.rptModule.DataBind();
                }
                else
                {
                    PageError("改角色不存在", "RoleList.aspx");
                }
            }
        }

        /// <summary>
        /// 转换布尔值为checked
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected string GetChecked(object obj)
        {
            if (obj.ToString() == true.ToString())
            {
                return "checked";
            }
            return "";
        }

        //protected string Convert(object obj)
        //{
        //    if (obj.ToString() == true.ToString())
        //    {
        //        return "是";
        //    }
        //    return "否";

        //}

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string roleId = Request.QueryString["roleId"].ToString();
            Role role = new Role();
            role.RoleID = roleId;

            //获取角色的操作权限
            List<OperatingRule> operatingRuleList = new List<OperatingRule>();
            string ids = Request["chkOperate"];
            if (!string.IsNullOrEmpty(ids))
            {
                string[] arraryIds = ids.Split(',');
                foreach (string Item in arraryIds)
                {
                    OperatingRule otr = new OperatingRule();
                    otr.OperatingRuleID = Item;
                    operatingRuleList.Add(otr);
                }
            }
            role.OperatingRuleList = operatingRuleList;

            //更新角色的操作权限
            bool isOk = ir.SetRoleOperatingRule(role);
            if (isOk)
            {
                PageInfo("角色操作权限更新成功！", "RoleList.aspx");
            }
            else
            {
                PageError("角色操作权限更新失败！", "RoleList.aspx");
            }
        }

        /// <summary>
        /// 绑定rptOperate
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptModule_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataTable table;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                table = this.allOperateTable;
                if (table.Rows.Count > 0)
                {
                    DataView dv = new DataView(table);
                    dv.RowFilter = "Module='" + DataBinder.Eval(e.Item.DataItem, "Module").ToString() + "' AND ComFlag='" + DataBinder.Eval(e.Item.DataItem, "ComFlag").ToString()
                        + "' AND CenFlag='" + DataBinder.Eval(e.Item.DataItem, "CenFlag").ToString() + "' AND HosFlag='" + DataBinder.Eval(e.Item.DataItem, "HosFlag").ToString() + "'";
                    Repeater rptStoreSubject = (Repeater)e.Item.FindControl("rptOperate");
                    rptStoreSubject.DataSource = dv;
                    rptStoreSubject.DataBind();
                }
            }
        }

        /// <summary>
        /// 合并单元格函数
        /// </summary>
        /// <param name="module"></param>
        /// <param name="repeatNum"></param>
        /// <returns></returns>
        protected string GetTDByModule(object module, object repeatNum)
        {
            string strTD = "";
            if (module.ToString() != lastModule)
            {

                strTD += "<td rowspan='" + Convert.ToString(repeatNum) + "' >";
                strTD += module.ToString();
                strTD += "</td>";
                
                //记住上次的Module
                lastModule = module.ToString();
            }
            return strTD;
        }
    }
}
